package com.example.evanproject.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.evanproject.config.UpdateImg;
import com.example.evanproject.domain.Trends;
import com.example.evanproject.service.TrendsService;
import com.example.evanproject.mapper.TrendsMapper;
import com.example.evanproject.util.EntityMapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;

/**
* @author hanxiaoqi
* @description 针对表【trends】的数据库操作Service实现
* @createDate 2024-03-09 21:52:13
*/
@Service
public class TrendsServiceImpl extends ServiceImpl<TrendsMapper, Trends>
    implements TrendsService{

    @Autowired
    private TrendsMapper trendsMapper;
    public List<Trends> filterbigpeople(Integer userid){
        return trendsMapper.filterbigpeople(userid);
    }


    //返回某个动态的详细信息

    public Map<String,Object> selectbyidinfo(Integer id)  {
        /*将本条记录与子级放到一个里面*/

        Map<String, Object> map = EntityMapUtils.entityToMap(trendsMapper.getlistbyid(id));
        map.put("childrenlist",trendsMapper.getParentChildTree(id));
        System.out.println(map);
        return map;
    }

    //发布课程 这是最外城的课程
    public Integer updatelistinfo(MultipartFile[] file, String text, Integer userid) {

//将上传的文件转化为一个字符串，存入数据库里面,在返回的时候 前端做判断
        String img="";
        StringJoiner joiner = new StringJoiner(",");
        if(file!=null){
            for(int i=0;i<file.length;i++){
                joiner.add(UpdateImg.updateimg(file[i]));
            }
            img=joiner.toString();
            System.out.println(img);
        }

//
//        获取当前时间
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        String time= dateFormat.format(date);
        return trendsMapper.updatelistinfo(time,text,userid,img);
    }

    public Integer updatecomment(Integer userid, Integer fid, String text) {
//        获取当前时间
        //获取当前时间带有时分秒
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        String time= dateFormat.format(date);
        return trendsMapper.updatecomment(userid,fid,text, time);
    }


    //    返回所有的动态
    public List<Trends> selectall() {
        return trendsMapper.selectall();
    }

    //    删除动态
    public Integer deletetrends(Integer treandid) {
        return  trendsMapper.deletetrends(treandid);
    }
}






